import Vue from 'vue'
import Router, { RouteConfig } from 'vue-router'
/* Layout */
import Layout from '@/layout/index.vue'
import AuthRedirect from '@/views/login/auth-redirect.vue'
// 各个子系统详情页,不附着在菜单上
import { systemAsyncRoutes } from './system'

import { config } from '@/microAppConfig'

Vue.use(Router)
// 配置说明
// 所有的详情页，只要是在本地配置的必须配置activeMenu属性，path对应对应高亮系统在左侧显示的父级菜单
/* meta: {
  title: 'title'               面包屑和导航栏显示导航字体
  icon: 'svg-name'             显示icon
  hidden: true                 if true, this route will not show in the sidebar (default is false)
  alwaysShow: true             if true, will always show the root menu (default is false)
                               if false, hide the root menu when has less or equal than one children route
  noCache: true                if true, the page will not be cached (default is false)
  affix: true                  if true, the tag will affix in the tags-view
  activeMenu: '/example/list'  详见navbar sidebar手动高亮函数
  showTooltip                   如果为true，显示tooltip，默认为false
  showTooltipContent            显示tooltip文字，前提是showTooltip为true
  showScreenFull                显示fullScreen控件
} */

const microRouters = config.map(it => {
  return {
    path: `${it.activeRule}/*`,
    component: Layout,
    meta: { hidden: true }
  }
})
export const constantRoutes: RouteConfig[] = [
  {
    path: '/redirect',
    component: Layout,
    meta: { hidden: true },
    children: [
      {
        path: '/redirect/:path*',
        component: () =>
          import(
            /* webpackChunkName: "redirect" */ '@/views/redirect/index.vue'
          )
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index.vue'),
    children: [
      {
        path: 'register',
        component: () => import('@/views/login/register.vue'),
        meta: {
          title: '新用户注册'
        }
      },
      {
        path: 'find-pwd',
        component: () => import('@/views/login/find-pwd.vue'),
        meta: {
          title: '找回密码'
        }
      },
      {
        path: 'reset-pwd',
        name: 'resetPwd',
        component: () => import('@/views/login/reset-pwd.vue'),
        meta: {
          title: '修改密码'
        }
      },
      {
        path: 'success',
        component: () => import('@/views/login/success.vue'),
        meta: {
          title: '注册成功'
        }
      },
      {
        path: 'success-email',
        name: 'successEmail',
        component: () => import('@/views/login/success-email.vue'),
        meta: {
          title: '邮件发送成功'
        }
      },
      {
        path: '/',
        component: () => import('@/views/login/login.vue')
      }
    ],
    meta: { title: '请登录 std-portal', hidden: true }
  },
  {
    path: '/',
    redirect: '/index',
    component: Layout,
    meta: {
      title: '...',
      hidden: true
    }
  },
  {
    path: '/auth-redirect',
    component: AuthRedirect,
    meta: {
      title: '...',
      hidden: true
    }
  },
  {
    path: '/guide',
    redirect: '/guide/index',
    component: Layout,
    children: [
      {
        path: 'index',
        component: () =>
          import(/* webpackChunkName: "guide" */ '@/views/guide/index.vue'),
        name: 'Guide',
        meta: {
          title: '引导页-星云管理后台',
          icon: 'guide',
          noCache: true,
          hidden: true
        }
      }
    ]
  },
  {
    path: '/profile',
    component: Layout,
    redirect: '/profile/index',
    meta: { hidden: true },
    children: [
      {
        path: 'index',
        component: () => import('@/views/profile/index.vue'),
        name: 'Profile',
        meta: {
          title: '个人信息',
          icon: 'user',
          noCache: true
        }
      }
    ]
  },
  ...microRouters
]
export const asyncRoutes: RouteConfig[] = [...systemAsyncRoutes]
const createRouter = () =>
  new Router({
    mode: 'history',
    scrollBehavior: (to, from, savedPosition) => {
      if (savedPosition) {
        return savedPosition
      } else {
        return { x: 0, y: 0 }
      }
    },
    base: process.env.BASE_URL,
    routes: constantRoutes
  })

const router: any = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  ;(router as any).matcher = (newRouter as any).matcher // reset router
}
export default router
